import request from '../utils/request'

/**
 * 获取角色列表
 * @returns promise
 */
export const getRolesListAPI = () => request('roles')

/**
 * 分配用户角色
 * @param {Number} id 用户ID
 * @param {Number} rid 角色ID
 * @returns promise
 */
export const dispatchRoleAPI = (id, rid) => request({
    method:'PUT',
    url:`users/${id}/role`,
    data:{
        rid
    }
})

/**
 * 添加角色
 * @param {Object} data {roleName, roleDesc}
 * @returns promise
 */
export const addRoleAPI = data => request({
    method:'post',
    url:'roles',
    data
})

/**
 * 删除角色
 * @param {Number} id 角色ID
 * @returns promise
 */
export const deleteRoleAPI = id => request({
    method:'delete',
    url:`roles/${id}`
})

/**
 * 编辑角色
 * @param {Object} data { id, roleName, roleDesc }
 * @returns promise
 */
export const editRoleAPI = data => request({
    method: 'put',
    url: `roles/${data.id}`,
    data:{
        roleName: data.roleName,
        roleDesc: data.roleDesc
    }
})

/**
 * 角色授权
 * @param {Number} id 角色ID
 * @param {string} rids 权限ID
 * @returns promise
 */
 export const setRoleRights = (id, rids) => request({
    method: 'post',
    url:`roles/${id}/rights`,
    data:{
        rids
    }
})

/**
 * 删除角色权限
 * @param {Number|string} roleId 角色ID
 * @param {Number|string} rightId 权限ID
 * @returns promise
 */
export const deleteRoleRights = (roleId, rightId) => request({
    method:'delete',
    url:`roles/${roleId}/rights/${rightId}`
})